package rhc.common.data.dao;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import rhc.common.entity.MyUrl;
import rhc.singleNodeSpider.BasicComponent;

/**爬虫url存储
 * @author rhc
 *
 */
public class SpiderUrlStorage extends BasicComponent{

	private String taskId;
	
	private SuperDao superDao;
	
	/**
	 * 抓取时间，该时间段内该url不会被再次抓取，
	 */
	private long catchTime=TimeUnit.MINUTES.toMillis(5);
	
	public SpiderUrlStorage(String taskId,SuperDao superDao){
		this.taskId=taskId;
		this.superDao=superDao;
	}
	
	/**获取 {urlCount} 个需要抓取的url
	 * @param urlCount
	 * @return
	 */
	public List<MyUrl> getUrlList(int urlCount){
		long currentMilis=System.currentTimeMillis();
		Query query=new Query(Criteria.where(MyUrl.COLUMN_TASK_ID).is(taskId).and(MyUrl.COLUMN_NEXT_UPDATE).lte(currentMilis));
		
		query.skip(0).limit(urlCount);
		
		List<MyUrl> urlList=superDao.getAllT(query, MyUrl.class);
		
		if(null != urlList && !urlList.isEmpty()){
			superDao.updateAll(query, new Update().inc(MyUrl.COLUMN_NEXT_UPDATE, catchTime), MyUrl.class);
		}
		
		return urlList;
	}
	
	/**增加一个url
	 * @param myUrl
	 * @throws Exception
	 */
	public void addUrl(MyUrl myUrl) throws Exception{
		if(null == myUrl){
			return;
		}
		
		myUrl.setTaskId(taskId);
		myUrl.setCreateDate(new Date());
		
		String uniqueCode=this.getMyUrlUniqueCode(myUrl);
		
		myUrl.setUniqueCode(uniqueCode);
		
		Query query=new Query(Criteria.where(MyUrl.COLUMN_UNIQUE_CODE).is(myUrl));
		
		Update update=new Update();
		update.setOnInsert(MyUrl.COLUMN_CREATE_DATE, myUrl.getCreateDate());
		update.setOnInsert(MyUrl.COLUMN_DATA, myUrl.getData());
		update.setOnInsert(MyUrl.COLUMN_HTTP_METHOD, myUrl.getHttpMethod());
		update.setOnInsert(MyUrl.COLUMN_NEXT_UPDATE, myUrl.getNextUpdate());
		update.setOnInsert(MyUrl.COLUMN_TASK_ID, myUrl.getTaskId());
		update.setOnInsert(MyUrl.COLUMN_UNIQUE_CODE, myUrl.getUniqueCode());
		update.setOnInsert(MyUrl.COLUMN_URL, myUrl.getUrl());
		
		superDao.updateFirst(query, update, MyUrl.class);
	}
	
	/**url是否已经存在
	 * @param myUrl
	 * @return
	 * @throws Exception 
	 */
	public boolean containsUrl(MyUrl myUrl) throws Exception{
		myUrl.setTaskId(taskId);
		
		String uniqueCode=this.getMyUrlUniqueCode(myUrl);
		
		MyUrl storageUrl=getUrlByUniqueCode(uniqueCode);
		
		return null!=storageUrl;
	}
	
	/**获取未抓取的url数量
	 * @return
	 */
	public long getNoCatchUrlCount(){
		long currentMilis=System.currentTimeMillis();
		Query query=new Query(Criteria.where(MyUrl.COLUMN_TASK_ID).is(taskId).and(MyUrl.COLUMN_NEXT_UPDATE).lte(currentMilis));
		
		return superDao.getQueryTotalCount(query, MyUrl.class);
	}
	
	/**根据url编码获取url
	 * @param uniqueCode
	 * @return
	 */
	private MyUrl getUrlByUniqueCode(String uniqueCode){
		return superDao.getOneT(new Query(Criteria.where(MyUrl.COLUMN_UNIQUE_CODE).is(uniqueCode)), MyUrl.class);
	}
	
	/**获取一个url的独特编码
	 * @param myUrl
	 * @return
	 */
	private String getMyUrlUniqueCode(MyUrl myUrl) throws Exception{
		
		String url=myUrl.getUrl();
		String httpMethod=myUrl.getHttpMethod();
		String data=myUrl.getData();
		String taskId=myUrl.getTaskId();
		
		if(StringUtils.isEmpty(url) || StringUtils.isBlank(url)){
			throw new IllegalArgumentException();
		}
		
		String code=url+String.valueOf(httpMethod)+String.valueOf(data);
		
		return code;
	}
	
	/**
	 * 清除资源引用
	 */
	public void close(){
		this.superDao=null;
	}
	
}
